package com.winapi.project.task;

import com.winapi.project.service.AlipayInfoService;
import com.winapi.project.service.OrderService;
import com.winapi.winapicommon.model.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yyzhang
 * @version 1.0
 */
@Slf4j
@Component
public class AlipayTask {
    @Resource
    private OrderService orderService;

    @Resource
    private AlipayInfoService alipayInfoService;

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm(){

        log.info("orderConfirm 被执行......");

        List<Orders> orderInfoList = orderService.getNoPayOrderByDuration(1);

        for (Orders orderInfo : orderInfoList) {
            String orderNumber = orderInfo.getOrderNumber();
            log.warn("超时订单 ===> {}", orderNumber);

            //核实订单状态：调用支付宝查单接口
            alipayInfoService.checkOrderStatus(orderNumber);
        }
    }
}
